namespace Factory.Service
{
    using System;
    using System.Security.Permissions;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using BadHabit.Factory;
    using BadHabit.Factory.Data;
    using BadHabit.Factory.Reports;

    /// <summary>
    /// Summary description for WebService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class ProjectService : System.Web.Services.WebService
    {
        public BadHabit.Factory.Service.FactorySOAPHeader FactoryHeader;

        /// <summary>
        /// Constructor.
        /// </summary>
        public ProjectService()
        {
            string url = Context.Request.Url.Scheme + "://" + Context.Request.Url.Authority + "/";

            // lets make this a singleton
            // this way i only read the factory configuration once
            FactoryLogic.CreateFactory(this.ServiceFilePath, url);
        }

        /// <summary>
        /// Add a new sprint to a given project.
        /// </summary>
        /// <param name="dsProject">The project dataset</param>
        /// <returns>The project with the new sprint</returns>
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "SCRUM_MASTER")]
        public ProjectDataSet RetriveSprintDefaultItems(string projectCategory)
        {
            return FactoryLogic.Current.RetriveSprintDefaultItems(projectCategory);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TeamDataSet RetriveAllMemberAllocation()
        {
            return FactoryLogic.Current.RetriveAllMemberAllocation();
        }

        #region General Methods

        /// <summary>
        /// Adds an amount of effort to a given task.
        /// </summary>
        /// <param name="taskUId">Task Uid</param>
        /// <param name="hours">Amount of hours</param>
        /// <param name="closeIt">True if the task should be closed</param>
        /// <returns>Any error message</returns>
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string ChangeTask(string oldTaskUId, double workedHours, bool closeIt, string newTaskUId)
        {
            return FactoryLogic.Current.ChangeTask(oldTaskUId, workedHours, closeIt, newTaskUId, FactoryLogic.Current.CurrentTeamMember);
        }

        /// <summary>
        /// Adds an amount of effort to a given task.
        /// </summary>
        /// <param name="taskUId">Task Uid.</param>
        /// <param name="hours">Amount of hours</param>
        /// <param name="closeIt">True if the task should be closed</param>
        /// <returns>Any error message</returns>
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string AddsActualEffort(string taskUId, double hours, bool closeIt)
        {
            return FactoryLogic.Current.AddsActualEffort(taskUId, hours, closeIt);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        public string GuideUrlForTemplate(string template, short status)
        {
            return string.Concat(new object[] 
            { 
                this.Context.Request.Url.GetLeftPart(UriPartial.Query).Replace("ProjectService.asmx", string.Empty).Replace("/GuideUrlForTemplate", string.Empty), "/Templates/", template, "/Guide/", status, ".htm" 
            });
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string[] RetriveAllClients()
        {
            return FactoryLogic.Current.RetriveAllClients();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public ProjectDataSet RetriveAllPayableProjects()
        {
            return FactoryLogic.Current.RetriveAllPayableProjects();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public ProjectDataSet RetriveAllProjectModels()
        {
            return FactoryLogic.Current.RetriveAllProjectModels();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public ProjectDataSet RetriveAllProjectPlataforms()
        {
            return FactoryLogic.Current.RetriveAllProjectPlataforms();
        }

        [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public ProjectDataSet RetriveAllProjects()
        {
            return FactoryLogic.Current.RetriveAllProjects();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public ProjectDataSet RetriveTeamMemberProjects(string teamMemberId)
        {
            return FactoryLogic.Current.RetriveTeamMemberProjects(teamMemberId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public ProjectDataSet RetriveRunningProjects()
        {
            return FactoryLogic.Current.RetriveRunningProjects();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public ProjectDataSet RetriveScheduledProjects()
        {
            return FactoryLogic.Current.RetriveScheduledProjects();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public ProjectDataSet RetriveToDoProjectsProjects(string userUId)
        {
            return FactoryLogic.Current.RetriveToDoProjects(userUId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TeamDataSet RetriveMembersByCompany(string ownerCompany)
        {
            return FactoryLogic.Current.RetriveMembersByCompany(ownerCompany);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public bool IsReadingClientContactsFromMsExchange()
        {
            return FactoryLogic.Current.IsReadingClientContactsFromMsExchange;
        }

        [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public ProjectDataSet RetriveClosedProjects()
        {
            return FactoryLogic.Current.RetriveClosedProjects();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public ProjectDataSet RetriveOportunitiesProjects()
        {
            return FactoryLogic.Current.RetriveOportunitiesProjects();
        }
        #endregion

        #region Commercial Methods
        /// <summary>
        /// Retrive still to pay projects.
        /// </summary>        
        /// <returns>project dataset</returns>
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
        [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
        public BadHabit.Factory.Data.ProjectDataSet RetriveStillToPayProjects()
        {
            return FactoryLogic.Current.RetriveStillToPayProjects();
        }

        /// <summary>
        /// Retrive in debit projects.
        /// </summary>        
        /// <returns>project dataset</returns>
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
        [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
        public BadHabit.Factory.Data.ProjectDataSet RetriveInDebitProjects()
        {
            return FactoryLogic.Current.RetriveInDebitProjects();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
        [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
        public ProjectDataSet RetriveProposalTemplate(string template)
        {
            return FactoryLogic.Current.RetriveProposalTemplate(this.ServiceFilePath + @".\App_Data\ProjectTemplates\" + template);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
        [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
        public string[] RetriveProposalTemplateList()
        {
            ProposalReport report = new ProposalReport(FactoryLogic.Current, null);
            return report.RetriveTemplateList();
        }

        /// <summary>
        /// Returns the proposal xaml.
        /// </summary>
        /// <param name="project">Project Dataset</param>
        /// <param name="templateName">Template Name</param>
        /// <returns>File DataSet</returns>   
        [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
        [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
        public string GenerateProposalXAML(ProjectDataSet project, string proposalUId, string templateName)
        {
            string approvedProposalXaml = FactoryLogic.Current.RetriveApprovedProposalXaml(proposalUId);
            if (approvedProposalXaml != null)
            {
                return approvedProposalXaml;
            }

            ProposalReport report = new ProposalReport(FactoryLogic.Current, proposalUId);
            return report.GenerateFlowDocumentXAML(project, templateName);
        }

        [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string GenerateTeamReportXAML(int month, int year)
        {
            ResourceReport report = new ResourceReport(FactoryLogic.Current, month, year);
            return report.GenerateFlowDocumentXAML("ResourceReport.xslt");
        }

        /// <summary>
        /// Aproves a given proposal for a project.
        /// </summary>
        /// <param name="project">The project</param>
        /// <param name="proposalUId">The proposal</param>
        /// <returns>Any error message</returns>
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
        [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
        public ProjectDataSet ApproveProposal(ProjectDataSet project, string proposalUId, out string alertMessage)
        {
            return FactoryLogic.Current.ApproveProposal(project, proposalUId, out alertMessage);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
        [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
        public ProjectDataSet RejectProposal(ProjectDataSet project, string proposalUId, out string alertMessage)
        {
            return FactoryLogic.Current.RejectProposal(project, proposalUId, out alertMessage);
        }

        #endregion

        #region Project Methods
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "SCRUM_MASTER")]
        [PrincipalPermission(SecurityAction.Demand, Role = "QUALITY_ASSURANCE")]
        public ProjectDataSet SaveProject(ProjectDataSet project, out string alertMessage)
        {
            return FactoryLogic.Current.PrepareToSaveProject(project, out alertMessage);
        }
        #endregion

        #region Report Methods
        [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string GenerateTailReportXAML(string projectUId, string templateName)
        {
            TailReport report = new TailReport(FactoryLogic.Current);
            report.ProjectUId = projectUId;
            return report.GenerateFlowDocumentXAML(templateName);
        }

        [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string GenerateHourBankReportXAML(string projectUId, string templateName)
        {
            if ((templateName == null) || (templateName == string.Empty))
            {
                templateName = "Default.xslt";
            }

            HourBankReport report = new HourBankReport(FactoryLogic.Current);
            report.ProjectUId = projectUId;
            return report.GenerateFlowDocumentXAML(templateName);
        }

        [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string GenerateBackLogReportXAML(string projectUId, string templateName)
        {
            if ((templateName == null) || (templateName == string.Empty))
            {
                templateName = "Standart Backlog.xslt";
            }

            BacklogReport report = new BacklogReport(FactoryLogic.Current);
            report.ProjectUId = projectUId;
            return report.GenerateFlowDocumentXAML(templateName);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public EmailDataSet RetriveHourBankReportEmail(string projectUId)
        {
            return FactoryLogic.Current.RetriveTemplateEmail(projectUId, @"\Proposal\HourBankReportEmail.xml");
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string[] RetriveHourBankReportTemplateList()
        {
            HourBankReport report = new HourBankReport(FactoryLogic.Current);
            return report.RetriveTemplateList();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string[] RetriveTailReportTemplateList()
        {
            TailReport report = new TailReport(FactoryLogic.Current);
            return report.RetriveTemplateList();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string[] RetriveBacklogReportTemplateList()
        {
            BacklogReport report = new BacklogReport(FactoryLogic.Current);
            return report.RetriveTemplateList();
        }

        #endregion

        #region Task Methods

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveTasksByTailedBacklogItemUId(string backlogItemUId)
        {
            return FactoryLogic.Current.RetriveTasksByTailedBacklogItemUId(backlogItemUId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveTasksByTailedProjectUId(string projectUId)
        {
            return FactoryLogic.Current.RetriveTasksByTailedProjectUId(projectUId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveTasksByTailUId(string tailUId)
        {
            return FactoryLogic.Current.RetriveTasksByTailUId(tailUId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveTasksByArtifactPath(string artifactPath)
        {
            return FactoryLogic.Current.RetriveTasksByArtifactPath(artifactPath);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public System.Data.DataSet RetriveOpenedTasks()
        {
            return FactoryLogic.Current.RetriveOpenedTasks();
        }

        [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveAllTasks()
        {
            return FactoryLogic.Current.RetriveAllTasks();
        }

        [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveProjectTasks(string projectUId, short releaseNumber)
        {
            return FactoryLogic.Current.RetriveProjectTasks(projectUId, releaseNumber);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveProjectDaillyTasks(string projectUId)
        {
            return FactoryLogic.Current.RetriveDaillyProjectTasks(projectUId);
        }

        [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveClosedProjectTasks(string projectUId, short releaseNumber)
        {
            return FactoryLogic.Current.RetriveClosedProjectTasks(projectUId, releaseNumber);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet.TaskResumeDataTable RetriveOpenedAssignedTasks(string taskAssignee)
        {
            return FactoryLogic.Current.RetriveOpenedAssignedTasks(taskAssignee);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveOpenedProjectTasks(string projectUId, short releaseNumber)
        {
            return FactoryLogic.Current.RetriveOpenedProjectTasks(projectUId, releaseNumber);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveOpenedProjectTasksForUser(string projectUId, string taskAssignee, short releaseNumber)
        {
            return FactoryLogic.Current.RetriveOpenedProjectTasksForUser(projectUId, taskAssignee, releaseNumber);
        }

        #endregion

        #region Tail Methods
        /// <summary>
        /// Creates a tail between a given task and a group of artifacts.
        /// </summary>
        /// <param name="taskId">The taskId</param>
        /// <param name="artifactsPath">The list of artifact to tail to the task</param>
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        public TaskDataSet TailTaskById(string repositoryUrl, int taskId, string[] artifactsPath, int revision)
        {
            return FactoryLogic.Current.TailTaskById(repositoryUrl, taskId, artifactsPath, revision);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        public string RetriveTeamMemberWorkingTaskUId(string teamMember)
        {
            return FactoryLogic.Current.GetWorkingTaskUIdByTeamMember(teamMember);
        }

        /// <summary>
        /// Creates a tail between current working task of a team member and a group of artifacts.
        /// </summary>
        /// <param name="teamMember">The team member</param>
        /// <param name="artifactsPath">The list of artifact to tail to the task</param>
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        public TaskDataSet TailTaskByTeamMember(string repositoryUrl, string teamMember, string[] artifactsPath, int revision)
        {
            return FactoryLogic.Current.TailTaskByTeamMember(repositoryUrl, teamMember, artifactsPath, revision);
        }

        /// <summary>
        /// Removes a task tail.
        /// </summary>    
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public void RemoveTaskTail(string taskTailUId)
        {
            FactoryLogic.Current.DeleteTaskTail(taskTailUId);
        }

        /// <summary>
        /// Removes a task tail.
        /// </summary>    
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public void RemoveTaskTailFromBacklogItem(string artifactPath, string backlogItemUId)
        {
            FactoryLogic.Current.DeleteTaskTail(backlogItemUId, artifactPath);
        }

        /// <summary>
        /// Removes a task tail.
        /// </summary>    
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public void TailTaskMaterialAsLink(string taskUId, string[] materialPath)
        {
            FactoryLogic.Current.TailTaskMaterial(taskUId, materialPath);
        }
        #endregion

        #region Team Member Methos
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        public void RefreshTeamMembers()
        {
            FactoryLogic.Current.LoadTeamMembers();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public void UpdateTeamMemberImage(byte[] imageBytes, string memberId)
        {
            FactoryLogic.Current.UpdateTeamMemberImage(imageBytes, memberId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public void UpdateMyTeamMemberImage(byte[] imageBytes)
        {
            FactoryLogic.Current.UpdateTeamMemberImage(imageBytes);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public BadHabit.Factory.Data.TeamDataSet RetriveMembersAtSameProject(string teamMemberId)
        {
            return FactoryLogic.Current.RetriveMembersAtSameProject(teamMemberId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TeamDataSet RetriveAllTeamMembers()
        {
            return FactoryLogic.Current.GetTeamMembers();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
        public void SaveTeamMemberList(TeamDataSet teamDataSet)
        {
            FactoryLogic.Current.SaveTeamMemberList(teamDataSet);
        }
        #endregion

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public void SaveTeamMemberProfile(TeamDataSet team)
        {
            FactoryLogic.Current.SaveTeamMemberProfile(team);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveAverageHoursBySizeItemType()
        {
            return FactoryLogic.Current.RetriveAverageHoursBySizeItemType();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        public ProjectDataSet RetriveProject(string projectUId)
        {
            return FactoryLogic.Current.RetriveProject(projectUId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        public ProjectDataSet RetriveProjectAndRolesOnly(string projectUId)
        {
            return FactoryLogic.Current.RetriveProjectAndRolesOnly(projectUId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        public ProjectDataSet RetriveProjectByNumber(int projectNumber)
        {
            return FactoryLogic.Current.RetriveProject(projectNumber);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "SCRUM_MASTER")]
        [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
        public ProjectDataSet RetriveProjectTemplate(string template)
        {
            return FactoryLogic.Current.RetriveProjectTemplate(this.ServiceFilePath + @".\App_Data\ProjectTemplates\" + template);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string[] RetriveProjectTemplateList()
        {
            return FactoryLogic.Current.RetriveProjectTemplateList(this.ServiceFilePath + @".\App_Data\ProjectTemplates");
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "COMMERCIAL_GUY")]
        [PrincipalPermission(SecurityAction.Demand, Role = "FACTORY_OWNER")]
        public EmailDataSet RetriveProposalEmail(string projectUId)
        {
            return FactoryLogic.Current.RetriveTemplateEmail(projectUId, @"\Proposal\ProposalEmail.xml");
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public double RetriveUserActualMonthEffort(string taskAssignee, int month, int year)
        {
            return FactoryLogic.Current.RetriveUserActualMonthEffort(taskAssignee, month, year);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        public string[] RetriveTeamMemberRoles()
        {
            string[] roles = null;
            if (System.Web.HttpContext.Current.Request.IsAuthenticated)
            {
                roles = FactoryLogic.Current.GetTeamMemberRoles(System.Threading.Thread.CurrentPrincipal.Identity.Name, null);
            }
            else
            {
                roles = FactoryLogic.Current.GetTeamMemberRoles(System.Threading.Thread.CurrentPrincipal.Identity.Name, this.FactoryHeader.UserAccessKey);
            }

            return roles;
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public double RetriveUserPlannedEffort(string taskAssignee)
        {
            return FactoryLogic.Current.RetriveUserPlannedEffort(taskAssignee);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet SaveTask(TaskDataSet task, out string alertMessage)
        {
            return FactoryLogic.Current.SaveTask(task, out alertMessage);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveTask(int taskId)
        {
            return FactoryLogic.Current.RetriveTask(taskId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string ValidateTask(TaskDataSet task)
        {
            string message = null;
            try
            {
                FactoryLogic.Current.ValidateTask(task);
            }
            catch (Exception exception)
            {
                message = exception.Message;
            }

            return message;
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        public string CurrentTeamMember()
        {
            return FactoryLogic.Current.CurrentTeamMember;
        }

        // Properties
        private string ServiceFilePath
        {
            get
            {
                return this.Context.Request.PhysicalPath.Substring(0, this.Context.Request.PhysicalPath.LastIndexOf(@"\"));
            }
        }

        /// <summary>
        /// Retrive the factory team velocity for a given month.
        /// </summary>
        /// <param name="date">month</param>
        /// <returns>The velocity for the given month</returns>
        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public double RetriveFactoryTeamVelocity()
        {
            return FactoryLogic.Current.RetriveFactoryTeamVelocity();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public ProjectDataSet RetriveBacklogItemUnits()
        {
            return FactoryLogic.Current.RetriveBacklogItemUnits();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveArtifactsByTaskUId(string taskUId)
        {
            return FactoryLogic.Current.RetriveArtifactsByTaskUId(taskUId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveArtifactsByBacklogItemUId(string backlogItemUId, string projectCategory)
        {
            return FactoryLogic.Current.RetriveArtifactsByBacklogItemUId(backlogItemUId, projectCategory);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveArtifactsByProjectUId(string projectUId, string projectCategory)
        {
            return FactoryLogic.Current.RetriveArtifactsByProjectUId(projectUId, projectCategory);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveImpactedItemsByTaskUId(string taskUId)
        {
            return FactoryLogic.Current.RetriveImpactedItemsByTaskUId(taskUId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveImpactedItemsByBacklogItemUId(string backlogItemUId)
        {
            return FactoryLogic.Current.RetriveImpactedItemsByBacklogItemUId(backlogItemUId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveImpactedItemsByProjectUId(string projectUId)
        {
            return FactoryLogic.Current.RetriveImpactedItemsByProjectUId(projectUId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public TaskDataSet RetriveImpactedItemsByTailUId(string tailUId)
        {
            return FactoryLogic.Current.RetriveImpactedItemsByTailUId(tailUId);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public ProjectDataSet RetriveBacklogItemSizes()
        {
            return FactoryLogic.Current.RetriveBacklogItemSizes();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public void RetriveProjectIndicators(string projectUId, ref double budgetIndicator, ref double qualityIndicator, ref double velocityIndicator, ref double deadlineIndicator, ref double sizeIndicator, ref double incomeIndicator, ref double walkedIndicator)
        {
            BadHabit.Factory.Data.ProjectPerformanceDataSet.ProjectPerformanceRow ind = FactoryLogic.Current.RetriveProjectPerformanceIndicators(projectUId);
            if (ind == null)
            {
                return;
            }

            budgetIndicator = ind.PctBudgetBurned;
            qualityIndicator = ind.PctBugEffort;
            velocityIndicator = ind.Velocity;
            deadlineIndicator = ind.HoursAhead;
            incomeIndicator = ind.IncomeRank;
            sizeIndicator = ind.SiteRank;
            walkedIndicator = ind.PctWalked;
        }

        /// <summary>
        /// Returns the proposal xaml.
        /// </summary>
        /// <param name="project">Project Dataset</param>
        /// <param name="templateName">Template Name</param>
        /// <returns>File DataSet</returns>   
        [WebMethod(BufferResponse = false), SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string GenerateProjectPerformanceReportXAML(int year, string member, string template)
        {
            ProjectPerformanceReport report;
            if (member == null)
            {
                report = new ProjectPerformanceReport(FactoryLogic.Current, year);
            }
            else
            {
                report = new ProjectPerformanceReport(FactoryLogic.Current, year, member);
            }

            return report.GenerateFlowDocumentXAML(template);
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string[] RetriveProjectPerformanceReportTemplateList()
        {
            ProjectPerformanceReport report = new ProjectPerformanceReport(FactoryLogic.Current, 0);
            return report.RetriveTemplateList();
        }

        [WebMethod, SoapHeader("FactoryHeader", Direction = SoapHeaderDirection.InOut)]
        [PrincipalPermission(SecurityAction.Demand, Role = "TEAM")]
        public string RetriveSourceControlViewUrl()
        {
            return FactoryLogic.Current.SourceControlViewUrl;
        }
    }
}